$(function() {
	var currentPart;
	jQuery(document).ready(function () {
		getPartInfo();
		realValueShow();
		setInterval(realValueShow,3000);
	});
//	function realValueShow() {
//		var partsele =Math.random();
//		var partsV = Math.random()/100;
//		var partsS = Math.round(Math.random()+Math.random()+Math.random());
//		if(Math.random()<0.5){
//			partsS=0-partsS;
//		}
//		partsele=487+partsele;
//		partsV=26.5+partsV;
//		jQuery('#partsele').val(partsele.toFixed(2));
//	 	jQuery('#partsV').val(partsV.toFixed(2));
//	 	jQuery('#partsH').val("优");
//	 	jQuery('#partsS').val(partsS);
//		
//	};
	function realValueShow() {
		var id = jQuery('#partId').val();
		var url=window.rootPath+"/ui/index/getPartRealInfo/"+id;
        jQuery.ajax({
            url:encodeURI(url),
            cache:false,
            type : "get",
            dataType:"json",
            success:function(data){
           	  	var count = data.count;
           	  	
           	  	$('#parts_name').val(data.parts_name);
           	  	$('#partsname').val(data.partsname);
           	  	$('#company_name').val(data.company_name);
           	  	$('#warnStatusName').val(data.warnStatusName);
           	  	$('#warnExp').val(data.warnExp);
           	  	$('#errorStatusName').val(data.errorStatusName);
           	  	$('#errorExp').val(data.errorExp);
           	  	$('#sendDate').val(data.sendDate);
           	  	$('#startDate').val(data.startDate);
           	  	$('#endDate').val(data.endDate);
           	  	$('#keepTime').val(data.keepTime);
           	  	
           	  	if(count > 0){
           	  		var last = false;
           	  		var html = '';
	           	  	for(var i=0;i<count;i++){
	           	  		var name = data['name'+i];
	           	  		var value = data['value'+i] != null ? data['value'+i] : '';
	           	  		var warning = data['warning'+i];
	           	  		var warningTitle = data['warningTitle'+i];
	           	  		var color ='';
	           	  		if(warning){
	           	  			color = 'red';
	           	  		}else{
	           	  			color = 'white';
	           	  		}
	           	  		
	           	  		if(last){
		           	  		html += '<label class="col-sm-2 control-label">'+name+'</label>'
	           	  				+ '<div class="col-sm-2">'
	           	  				+ '<input type="text" class="form-control indexPartsInput" id="value'+i+'" value="'+value+'"/>'
	      					    + '</div>'
	      					    + '<label class="col-sm-2" id="warning'+i+'"><font style="color:'+color+';" id="warningTitle'+i+'">'+warningTitle+'</font></label>'
	      					    + '</div>';
	           	  		}else{
	           	  			html += '<div class="form-group">'
	           	  				+ '<label class="col-sm-2 control-label">'+name+'</label>'
	           	  				+ '<div class="col-sm-2">'
	           	  				+ '<input type="text" class="form-control indexPartsInput" id="value'+i+'" value="'+value+'"/>'
	      					    + '</div>'
	      					    + '<label class="col-sm-2" id="warning'+i+'"><font style="color:'+color+';" id="warningTitle'+i+'">'+warningTitle+'</font></label>';
	      					    
	           	  		}
	           	  		last = !last;
	           	  	}
	           	  	if(last){
	           	  		html += '</div>';
	           	  	}
	           	  	$('#partRealValue').html(html);
           	  	}
           	 	
            }
        });
	};
	jQuery('#realTimeTVtab').on('click', function () {
		if(currentPart){
			CreateTV(currentPart,200,false);
		}
	});
	function CreateTV(part,interval,isInstall)
	{
		var isIECore='';
		if(!!window.ActiveXObject || "ActiveXObject" in window)
		    isIECore=true;
		else  
		    isIECore=false;
		
		if (isIECore) {
			if(isInstall){
				WatchTV(part.interfaceAddress,part.interfacePort); 
			}
		}
		else {
			$('#realTimeTV').empty();
			setTimeout(function(){
				$('#realTimeTV').append('<object id="csocx" clsid="{59EA8A50-4FC1-406a-A471-595247AB2E41}" type="application/x-itst-activex"  progid="/wcsi/tvfile/CISDITV.cab" width="100%" height="415px"></object>');
				WatchTV(part.interfaceAddress,part.interfacePort);	
			},interval);
		}
	};
	function WatchTV(ip,channel)
	{
		var ocxKq = document.getElementById("csocx");
        //ocxKq.TVView(ip,channel); 
		ocxKq.InstallView(ip,channel); 
		ocxKq.OpenStream();
	};
	function getPartInfo(){
		var id = jQuery('#partId').val();
		 var url=window.rootPath+"/ui/index/getPartInfo/"+id;
         jQuery.ajax({
             url:encodeURI(url),
             cache:false,
             type : "get",
             dataType:"json",
             success:function(data){
            	  	var part = data.part;
            	  	currentPart=data.part;
            	  	if(part.part&&part.part.partType&&part.part.partType.value==1){
            	  		CreateTV(part,0,true);
            	  	}else{
            	  		$('#realTimeTVtab').remove();
            	  		$('#realTimeTV').remove();
            	  		$('#baseMsgtab').addClass("active");
            	  		$('#baseMsg').addClass("active");
            	  	}
            	 	jQuery('#deviceIdentify').val(part.deviceIdentify);
            	 	jQuery('#partName').val(part.partName);
//            	 	jQuery('#partName2').val(part.partName);
            	 	jQuery('#manufacturer').val(part.manufacturer);
            	 	jQuery('#productionTime').val(moment(part.productionTime).format('YYYY/MM/DD'));
            	 	jQuery('#installationTime').val(moment(part.installationTime).format('YYYY/MM/DD'));
            	 	if(part.gridAreaInfo){
            	 		jQuery('#gridAreaInfo').text(part.gridAreaInfo.gridName);
            	 	}
            	 	jQuery('#partIdentify').val(part.partIdentify);
            	 	
            	 	var partIcon = part.iconAddress;
            	 	if(typeof(partIcon) != 'undefined' &&  partIcon != ''){
            	 		partIcon = window.rootPath +'/'+partIcon;
            	 	}else{
            	 		partIcon = window.rootPath+"/img/nopicture.png";
            	 	}
            	 	jQuery('#iconAddress').attr('src',partIcon);
            	 	
            	 	var state = part.state.title;
            	 	if(state == '有效'){
            	 		jQuery('#stateIcon').attr('src',window.rootPath+"/img/images/正常.png");
            	 	}else{
            	 		jQuery('#stateIcon').attr('src',window.rootPath+"/img/images/故障.png");
            	 	}
            	 		
        	 		jQuery('#state').text(state);
        	 		var longitude = part.longitude;
        	 		var latitude = part.latitude;
        	 		var longitudeFormat = longFormatDegree(longitude);
        	 		var latitudeFormat = latFormatDegree(latitude);
        	 		var longlat=longitudeFormat+','+latitudeFormat;
        	 		jQuery('#longlat').val(longlat);

        	 		var partType = part.part.partType.value;
        	 	/*	jQuery('#interfaceAddress').val('');
    				jQuery('#interfacePort').val('');*/
        			if (partType == '1') {
        				/*jQuery('#interfaceAddress').val(part.interfaceAddress);
        				jQuery('#interfacePort').val(part.interfacePort);*/
        				jQuery('#interfaceDiv').show();
        			} else {
        				jQuery('#interfaceDiv').hide();
        			}
             }
         });
	}
	
	//将度转换成为度分秒
	function longFormatDegree(value) {
		var long ='E';
		if(value < 0){
			long = 'W'
		}
        value = Math.abs(value);
        var v1 = Math.floor(value);//度
        var v2 = Math.floor((value - v1) * 60);//分
        var v3 = ((value - v1) * 3600 % 60).toFixed(2);//秒
        return long + v1 + '°' + v2 + '\'' + v3 + '"';
    };
    
    function latFormatDegree(value) {
    		var lat ='N';
		if(value < 0){
			lat = 'S'
		}
	    	value = Math.abs(value);
	    	var v1 = Math.floor(value);//度
	    	var v2 = Math.floor((value - v1) * 60);//分
	    	var v3 = ((value - v1) * 3600 % 60).toFixed(2);//秒
	    	return lat + v1 + '°' + v2 + '\'' + v3 + '"';
    };
});
